
package dk.unet.android.adaptors;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import dk.unet.android.ll11.R;
import dk.unet.android.types.InformationPiece;
import java.util.List;

/**
 *
 * @author Nicolai Willems
 */
public class InformationListAdapter extends ArrayAdapter<InformationPiece> {
    static class ViewHolder{
        TextView title, content;
    }

    private LayoutInflater inflater;
    private Context ctx;

    public InformationListAdapter(Context ctx){
        super(ctx, R.layout.info_item);
        this.ctx = ctx;
        this.inflater = LayoutInflater.from(ctx);
    }

    public InformationListAdapter(Context context, InformationPiece[] objects) {
        super(context, R.layout.info_item, objects);
        this.inflater = LayoutInflater.from(context);
        this.ctx = context;
    }

    public InformationListAdapter(Context context, int complexViewResourceId) {
        super(context, complexViewResourceId);
        this.inflater = LayoutInflater.from(context);
        this.ctx = context;
    }

    public InformationListAdapter(Context context, int complexViewResourceId, InformationPiece[] objects) {
        super(context, complexViewResourceId, objects);
        this.inflater = LayoutInflater.from(context);
        this.ctx = context;
    }
    
    public InformationListAdapter(Context context, int complexViewResourceId, List<InformationPiece> objects) {
        super(context, complexViewResourceId, objects);
        this.inflater = LayoutInflater.from(context);
        this.ctx = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if(convertView == null){
            convertView = inflater.inflate(R.layout.info_item, null);

            holder = new ViewHolder();
            holder.title = (TextView)convertView.findViewById(R.id.InformationTitle);
            holder.content = (TextView)convertView.findViewById(R.id.InformationContent);

            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }

        InformationPiece ip = super.getItem(position);

        if(ip != null){
            Log.d("InformationListAdapter", "Got IP with following, "+position+"=>("+ip.getTitle()+","+ip.getContent()+")");
        }else{
            Log.d("InformationListAdapter", "No informatrion was retrieved for position: "+position);
        }

        holder.title.setText(ip.getTitle());
        holder.content.setText(ip.getContent());
        
        return convertView;
    }
}
